package com.xinkong.lib_common.base

import android.content.Context
import android.support.annotation.IdRes
import android.support.v4.app.Fragment
import com.xinkong.lib_common.util.Utils
import com.xinkong.lib_common.util.checkNotNull

/**
 * @author  新空
 * @time  2018/8/15/015 21:40
 * @week 星期三
 */
abstract class BaseFragment : Fragment() {
    protected lateinit var mActivity: BaseActivity

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        this.mActivity = context as BaseActivity
    }


    /**
     * 获取宿主Activity
     *
     * @return BaseActivity
     */
    protected fun getHoldingActivity(): BaseActivity {
        return mActivity
    }


    /**
     * 添加fragment
     *
     * @param fragment
     * @param frameId
     */
    protected fun addFragment(fragment: BaseFragment, @IdRes frameId: Int) {
        checkNotNull(fragment)
        getHoldingActivity().addFragment(fragment, frameId)

    }


    /**
     * 替换fragment
     *
     * @param fragment
     * @param frameId
     */
    protected fun replaceFragment(fragment: BaseFragment, @IdRes frameId: Int) {
        checkNotNull(fragment)
        getHoldingActivity().replaceFragment(fragment, frameId)
    }


    /**
     * 隐藏fragment
     *
     * @param fragment
     */
    protected fun hideFragment(fragment: BaseFragment) {
        checkNotNull(fragment)
        getHoldingActivity().hideFragment(fragment)
    }


    /**
     * 显示fragment
     *
     * @param fragment
     */
    protected fun showFragment(fragment: BaseFragment) {
        checkNotNull(fragment)
        getHoldingActivity().showFragment(fragment)
    }


    /**
     * 移除Fragment
     *
     * @param fragment
     */
    protected fun removeFragment(fragment: BaseFragment) {
        checkNotNull(fragment)
        getHoldingActivity().removeFragment(fragment)

    }


    /**
     * 弹出栈顶部的Fragment
     */
    protected fun popFragment() {
        getHoldingActivity().popFragment()
    }
}